package com.lqq.controller.restfull;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author lqq
 * @date 2019/1/6
 */
@RestController
@Api(description = "index")
public class IndexController {

    ExecutorService executorService = Executors.newFixedThreadPool(8);

    @RequestMapping({"", "/" , "/index"})
    @ApiOperation(value = "index",httpMethod = "GET")
    public String index() throws UnknownHostException {
        InetAddress address = InetAddress.getLocalHost();
        return  "indexMethod: 当前主机ip地址信息：" + address.getHostAddress();
    }

    @RequestMapping({"/hello"})
    @ApiOperation(value = "hello",httpMethod = "GET")
    public String getIP() throws UnknownHostException {
        InetAddress address = InetAddress.getLocalHost();
        //System.out.println(address.getHostName());//主机名
        //System.out.println(address.getCanonicalHostName());//主机别名
        //System.out.println(address.getHostAddress());//获取IP地址
        return  "helloMethod: 当前主机ip地址信息：" + address.getHostAddress();
    }

    @RequestMapping({"/alarm"})
    public void alarm(@RequestBody List<AlarmMessage> alarmMessageList) {
        System.out.println(alarmMessageList.toString());

        String url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=12a1d36e-b789-4e7f-8d09-df1b3f512885";
        JSONObject content = new JSONObject();
        content.put("content", JSON.toJSONString(alarmMessageList));

        JSONObject param = new JSONObject();
        param.put("msgtype","text");
        param.put("text",content);

        HttpUtil.post(url, param.toJSONString(), 2000);
    }

    @RequestMapping({"/deadWhile"})
    public void deadWhile() {
        for(int i=0;i<7;i++){
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    while (true){
                        // System.out.println("0");
                    }
                }
            });
        }

    }
}
